package com.example.service;

import cn.hutool.core.util.StrUtil;
import com.example.domain.Account;
import com.example.domain.Admin;
import com.example.domain.Employee;
import com.example.exception.CustomException;
import com.example.mapper.EmployeeMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.List;

@Service
public class EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;

    //    查全部数据
    public List<Employee> selectAll(Employee employee) {
        return employeeMapper.selectAll(employee);
    }

    //    查单个数据
    public Employee selectByPrimaryKey(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    //    分页查询
    public PageInfo<Employee> selectPage(Employee employee, Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Employee> list = employeeMapper.selectAll(employee);
        return PageInfo.of(list);
    }

    // 新增数据---注册
    public void add(Employee employee) {
        String username = employee.getUsername();
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee != null){    //账号存在，无法注册
            throw new CustomException("500","账号存在，无法注册");
        }
        if (StrUtil.isBlank(employee.getPassword())){   //密码没填
            employee.setPassword("12345678");    //默认密码123
        }
//        if (StrUtil.isBlank(employee.getName())){   //名字没填
//            employee.setPassword("张三");    //默认名字
//        }
        employee.setRole("EMP");    //设置角色

        employeeMapper.insert(employee);
    }

    // 更新数据
    public void update(Employee employee) {

        employeeMapper.updateByPrimaryKey(employee);

    }

    //根据id  删除数据
    public void deleteById(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    //    员工用户登录
    public Employee login(Account account) {
        String username = account.getUsername();    //  判断账号是否正确
        Employee dbEmployee = employeeMapper.selectByUsername(username);
        if (dbEmployee == null){    //没查询到用户
            throw new CustomException("500","没有此用户");
        }

        String password = account.getPassword();
        if (!dbEmployee.getPassword().equals(password)){    //密码错误
            throw new CustomException("500","密码错误");
        }
        return dbEmployee;
    }


//检验注册账号是否重复
    public void register(Employee employee) {
        this.add(employee);
    }



//  iter      批量删除---循环调用单个id删除
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            this.deleteById(id);
        }
    }

    //员工登录修改密码
    public void updatePassword(Account account) {
            Integer id = account.getId();
            Employee employee = this.selectByPrimaryKey(Long.valueOf(id));
            if (!employee.getPassword().equals(account.getPassword())){     //页面传来的原密码跟数据库对比，不匹配就报错
                throw new CustomException("500","对不起，原密码错误");
            }
            employee.setPassword(account.getNewPassword());         //设置新密码

            this.update(employee);

    }

}
